/**
 * 合同contract.js
 * 版本号： v1.0.0
 */
$(function () {
    var lastChecked = null;
    $("#contract_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/contract/contractList.do',
        idField: 'coId',
        frozenColumns: [[
            {field: 'coId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 260, fixed: true},
                {field: 'coName', title: '合同名称', width: 260, fixed: true},
                {field: 'coNumber', title: '合同编号', width: 260, fixed: true},
                {field: 'prLeader',title: '项目负责人',width: 80,fixed: true},
                {field: 'prRealStart', title: '项目开工日期', width: 120}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#contract_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#contract_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#contract_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#contract_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.coId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            var row = $("#contract_dataList").datagrid("getSelected");
                            window.location.href = CONTEXT_PATH + "/contractDetail/toContractDetailAdd.do?coId="+row.coId;
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.coId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            contract_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.coId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            contract_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //清单按钮
                if("contractDetailBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.coId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            contractDetailList();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 合同清单列表
 */
function contractDetailList(){
    var row = $("#contract_dataList").datagrid("getSelected");
    // window.location.href = CONTEXT_PATH + "/contractDetail/contractDetailInventoryListIndex.do?coId="+row.coId;

    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的合同清单列表", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(htqdlb.id+"contractDetailList",htqdlb.url + row.coId, htqdlb.iconCls, htqdlb.closable,htqdlb.title);
}

//查询合同
function contract_findList() {
    refreshPageNumber("contract_dataList");
    $("#contract_dataList").datagrid("reload",{
        prName:$("#prName_").val(),
        coName:$("#coName_").val(),
        coNumber:$("#coNumber_").val()
    });
    //清楚选中项
    $("#contract_dataList").datagrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param coId
 */
function refreshPageNumber(coId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + coId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + coId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看合同详情
 */
function contract_view(iconCls){
    var row = $("#contract_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的合同详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(htqdhtxq.id,htqdhtxq.url + row.coId, iconCls, htqdhtxq.closable,htqdhtxq.title);
}
